﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

using CustomControlDatabaseInterface.Utilities;

namespace CustomControlDatabaseInterface.ViewModel
{
    public class CustomerDetailAddControlViewModel : ViewModelBase {


        private bool isAddingEnabled; 
        public bool IsAddingEnabled {
            get {
                return isAddingEnabled;
            }
            set {
                isAddingEnabled = value; 
                OnPropertyChanged("IsAddingEnabled");
            }
        }
       

        public ObservableCollection<String> AvailableControls { get; set; }

        private String selectedControl; 
        public String SelectedControl {
            get {
                return this.selectedControl; 
            }
            set {
                if (value != null) {
                    IsAddingEnabled = true;
                }
                else {
                    IsAddingEnabled = false;
                }
                selectedControl = value; 
                OnPropertyChanged("SelectedControl");
            }
        }

        public ICommand AddControlCommand { get; set; }

        public delegate void AddControlEventHandler(String control);

        public event AddControlEventHandler AddControl;

        public ICommand CancelCommand { get; set; }

        public delegate void CancelEventHandler();

        public event CancelEventHandler Cancel; 

        public CustomerDetailAddControlViewModel() {

            AvailableControls = new ObservableCollection<string>();

            AddControlCommand = new RelayCommand(param => OnAddControlCommand());
            CancelCommand = new RelayCommand(param => OnCancelCommand());
        }

        public void InitializeAddControlViewModel(List<String> ImplementedControls, String technology) {
            IsAddingEnabled = false;
            List<String> allControls = DatabaseUtil.GetTableHeaders(technology); 
            UpdateObservableCollection(AvailableControls, allControls.Except(ImplementedControls).ToList());
            AvailableControls.RemoveAt(0);
        }

        private void OnAddControlCommand() {
            AddControlEventHandler handler = AddControl;
            if (handler != null) {
                handler(SelectedControl);
            }
        }

        private void OnCancelCommand() {
            IsAddingEnabled = false; 
            CancelEventHandler handler = Cancel; 
            if (handler != null) {
                handler(); 
            }
        }


    }
}
